package com.kt.uokos.yimo.service.impl;

import com.github.pagehelper.PageHelper;
import com.kt.uokos.yimo.dao.AdminNotice;
import com.kt.uokos.yimo.example.AdminNoticeExample;
import com.kt.uokos.yimo.example.NoticeExample;
import com.kt.uokos.yimo.mapper.AdminNoticeMapper;
import com.kt.uokos.yimo.service.AdminNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @ClassName AdminNoticeService
 * @Date 2021/12/27 上午11:58
 * @Author KangTao
 */
@Service
public class AdminNoticeServiceImpl implements AdminNoticeService {

    @Autowired
    private AdminNoticeMapper adminNoticeMapper;

    @Override
    public void add(AdminNotice noticeAdmin) {
        noticeAdmin.setAddTime(LocalDateTime.now());
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        adminNoticeMapper.insertSelective(noticeAdmin);
    }

    @Override
    public List<AdminNotice> queryByNoticeId(Integer noticeId) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andNoticeIdEqualTo(noticeId).andDeletedEqualTo(false);
        return adminNoticeMapper.selectByExample(example);
    }

    @Override
    public List<AdminNotice> querySelective(String title, String type, Integer adminId, Integer page, Integer limit, String sort, String order) {
        AdminNoticeExample example = new AdminNoticeExample();
        AdminNoticeExample.Criteria criteria = example.createCriteria();

        if(!StringUtils.isEmpty(title)){
            criteria.andNoticeTitleLike("%" + title + "%");
        }

        if(type.equals("read")){
            criteria.andReadTimeIsNotNull();
        }
        else if(type.equals("unread")){
            criteria.andReadTimeIsNull();
        }
        criteria.andAdminIdEqualTo(adminId);
        criteria.andDeletedEqualTo(false);

        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return adminNoticeMapper.selectByExample(example);
    }

    @Override
    public int countUnread(Integer adminId) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andAdminIdEqualTo(adminId).andReadTimeIsNull().andDeletedEqualTo(false);
        return (int)adminNoticeMapper.countByExample(example);
    }

    @Override
    public int countReadByNoticeId(Integer noticeId) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andNoticeIdEqualTo(noticeId).andReadTimeIsNotNull().andDeletedEqualTo(false);
        return (int)adminNoticeMapper.countByExample(example);
    }

    @Override
    public void updateByNoticeId(AdminNotice adminNotice, Integer noticeId) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andNoticeIdEqualTo(noticeId).andDeletedEqualTo(false);
        adminNotice.setUpdateTime(LocalDateTime.now());
        adminNoticeMapper.updateByExampleSelective(adminNotice, example);
    }

    @Override
    public void deleteByNoticeId(Integer id) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andNoticeIdEqualTo(id).andDeletedEqualTo(false);
        AdminNotice noticeAdmin = new AdminNotice();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);
        adminNoticeMapper.updateByExampleSelective(noticeAdmin, example);
    }

    @Override
    public void deleteByNoticeIds(List<Integer> ids) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andNoticeIdIn(ids).andDeletedEqualTo(false);
        AdminNotice noticeAdmin = new AdminNotice();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);
        adminNoticeMapper.updateByExampleSelective(noticeAdmin, example);
    }

    public AdminNotice find(Integer noticeId, Integer adminId) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andNoticeIdEqualTo(noticeId).andAdminIdEqualTo(adminId).andDeletedEqualTo(false);
        return adminNoticeMapper.selectOneByExample(example);
    }

    @Override
    public void update(AdminNotice noticeAdmin) {
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        adminNoticeMapper.updateByPrimaryKeySelective(noticeAdmin);
    }


    @Override
    public void markReadByIds(List<Integer> ids, Integer adminId) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andIdIn(ids).andAdminIdEqualTo(adminId).andDeletedEqualTo(false);
        AdminNotice noticeAdmin = new AdminNotice();
        noticeAdmin.setReadTime(LocalDateTime.now());
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        adminNoticeMapper.updateByExampleSelective(noticeAdmin, example);
    }

    @Override
    public void deleteById(Integer id, Integer adminId) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andIdEqualTo(id).andAdminIdEqualTo(adminId).andDeletedEqualTo(false);
        AdminNotice noticeAdmin = new AdminNotice();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);
        adminNoticeMapper.updateByExampleSelective(noticeAdmin, example);
    }

    @Override
    public void deleteByIds(List<Integer> ids, Integer adminId) {
        AdminNoticeExample example = new AdminNoticeExample();
        example.or().andIdIn(ids).andAdminIdEqualTo(adminId).andDeletedEqualTo(false);
        AdminNotice noticeAdmin = new AdminNotice();
        noticeAdmin.setUpdateTime(LocalDateTime.now());
        noticeAdmin.setDeleted(true);
        adminNoticeMapper.updateByExampleSelective(noticeAdmin, example);
    }
}
